package com.awesome.arouterdemo.activities;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.callback.NavCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.awesome.arouterdemo.Constance;
import com.awesome.arouterdemo.R;
import com.awesome.common.util.log.ShowLogUtil;

import static com.awesome.arouterdemo.activities.SecondActivity.FLAG;
import static com.awesome.arouterdemo.activities.SecondActivity.GREET;
import static com.awesome.arouterdemo.activities.SecondActivity.NAME;
import static com.awesome.arouterdemo.activities.SecondActivity.RESULT_CODE;

public class MainActivity extends AppCompatActivity {
    public static final int REQUEST_CODE = 0x01;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 跳转带返回的Activity
        findViewById(R.id.tv_jump).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // build第一个参数跳转uri，第二个参数分组
                // withString传参
                // navigation第二个参数带返回Intent，第三个参数拦截回调
                ARouter.getInstance()
                        .build(Constance.ACTIVITY_URL_SECOND, Constance.GROUP_FIRST)
                        .withString(NAME, "android")
                        .withBoolean(FLAG, true)
                        .navigation(MainActivity.this, REQUEST_CODE, new NavCallback() {
                            // 拦截处理之前的方法
                            @Override
                            public void onFound(Postcard postcard) {
                                super.onFound(postcard);
                                ShowLogUtil.info("onFound:getGroup:" + postcard.getGroup() + ",getPath:" + postcard.getPath());
                            }

                            // 拦截处理之后的方法
                            @Override
                            public void onArrival(Postcard postcard) {
                                ShowLogUtil.info("onArrival:getGroup:" + postcard.getGroup() + ",getPath:" + postcard.getPath());
                            }
                        });

            }
        });
        // 跳转Fragment
        findViewById(R.id.tv_jump_fragment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance()
                        .build(Constance.ACTIVITY_URL_MY)
                        .navigation();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_CODE) {
            if (data != null)
                ShowLogUtil.info(data.getStringExtra(GREET));
        }
    }
}